package com.maximus.cli.system;

import com.alibaba.fastjson2.JSON;
import com.maximus.cli.CliMain;
import com.maximus.cli.domain.Commands;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

public class SystemEnvironment {
    private static final Logger logger = LogManager.getLogger(CliMain.class);

    public static Map<String, Commands> COMMANDS_MAP;

    public static void initCommands(String file) throws IOException {
        try {
            String contentJson = new String(Files.readAllBytes(Paths.get(file)), StandardCharsets.UTF_8);
            List<Commands> commands = JSON.parseArray(contentJson, Commands.class);

            for (Commands command : commands) {
                COMMANDS_MAP.put(command.getName(), command);
            }

        } catch (IOException ioException) {
            logger.info("命令行文件加载异常");
            throw ioException;
        }
    }
}
